const app = getApp()
var CONFIG = require("../../../../config/config.js");
var share = require('../../../../utils/share.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        taparr: [{ id: 1, title: '上课安排' }, { id: 2, title: '书籍信息' }],
        tap_id: 1,
        student_id: '',
        code: -1,
        studec: '',
        booklist: [],
        node: '',
        node1: '',
        nodefalse: false,
        zb_false: false,
        zb_arr: [],
        zb_item: '',
        student_id1: '',
    },
    yesclcik() {
        var that = this;
        if (that.data.student_id1 != '') {
            wx.showLoading({
                title: '加载中~',
            })
            that.setData({
                zb_false: false,
            })
            var obj = {
                student_id: that.data.student_id,
                class_id: that.data.student_id1,
            }
            share.wxRequest('crmapi/user/updStudent', obj, function(res) {
                wx.hideLoading();
                wx.showToast({
                    title: res.data.msg,
                    icon: 'none'
                })
                if (Number(res.data.errcode) == 0) {
                    setTimeout(function() {
                        that.stuClassDis();
                    }, 500)
                }
            })
        } else {
            wx.showToast({
                title: '请先选择班级',
                icon: 'none'
            })
        }
    },
    changeclass(e) {
        var that = this;
        // console.log(e);
        var item = e.target.dataset.item || e.currentTarget.dataset.item;
        var index = e.target.dataset.index || e.currentTarget.dataset.index;
        var zb_arr = that.data.zb_arr;
        for (var i = 0; i < zb_arr.length; i++) {
            if (Number(i) == Number(index)) {
                zb_arr[i].choose_ch = 2;
            } else {
                zb_arr[i].choose_ch = 1;
            }
        }
        that.setData({
            zb_arr: zb_arr,
            student_id1: that.data.zb_arr[index].id,
        })
    },
    zclass() {
        var that = this;
        wx.showLoading({
            title: '加载中~',
        })
        that.setData({
            zb_arr: [],
            student_id1: '',
        })
        that.chaClassList()
    },
    chaClassList() {
        var that = this;
        share.wxRequest('crmapi/user/chaClassList', { student_id: that.data.student_id }, function(res) {
            if (Number(res.data.errcode) == 0) {
                var data = res.data.data;
                if (Number(data.lists.length) > 0) {
                    for (var i = 0; i < data.lists.length; i++) {
                        data.lists[i]['choose_ch'] = 1;
                    }
                    that.setData({
                        zb_arr: data.lists,
                        zb_false: true,
                    })
                } else {
                    wx.showToast({
                        title: '暂无可转班数据',
                        icon: 'none'
                    })
                }
            }
            wx.hideLoading();
        })
    },
    closeclcik() {
        var that = this;
        that.setData({
            zb_false: false,
        })
    },
    noclick() {
        this.setData({
            nodefalse: false,
        })
    },
    changenote() {
        this.setData({
            nodefalse: true,
        })
    },
    // /点击提交备注
    bindFormSubmit(e) {
        var that = this;
        var value = e.detail.value.textarea;
        if (value == '') {
            wx.showToast({
                title: '请输入备注',
                icon: 'none',
                duration: 1500
            })
        } else {
            wx.showLoading({
                title: '加载中~',
            })
            var objdata = {
                student_id: that.data.student_id, //	是	int	班级学员ID
                note: value, //	否	int	备注
            }
            share.wxRequest('crmapi/user/classNote', objdata, function(res) {
                if (Number(res.data.errcode) == 0) {
                    wx.showToast({
                        title: '修改成功',
                        icon: 'none',
                        duration: 1500
                    })
                    that.setData({
                        node: value,
                        node1: value,
                        nodefalse: false,
                    })
                } else {
                    wx.showToast({
                        title: res.data.msg,
                        icon: 'none',
                        duration: 1500
                    })
                }
            })
        }
    },
    clicktap(e) {
        var that = this;
        var item = e.target.dataset.item || e.currentTarget.dataset.item;
        that.setData({
            tap_id: item.id
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this;
        that.setData({
            student_id: options.student_id
        })
        that.stuClassDis();
    },

    stuClassDis() {
        var that = this;
        wx.showLoading({
            title: '加载中~',
        })
        var objdata = {
            consultant_id: app.globalData.admin_id, //是	int	当前管理员ID
            student_id: that.data.student_id, //是	int	班级学员ID
        }
        share.wxRequest('crmapi/user/stuClassDis', objdata, function(res) {
            if (Number(res.data.errcode) == 0) {
                var data = res.data.data;
                // console.log(data)
                that.setData({
                    studec: data.lists,
                    code: res.data.errcode
                })
                if (data.lists.note != '') {
                    that.setData({
                        node: data.lists.note,
                        node1: data.lists.note,
                    })
                } else {
                    that.setData({
                        node: '',
                        node1: '',
                    })
                }
                that.stuBook(data.lists.user_id)
            } else {
                that.setData({
                    code: 1002
                })
            }
            wx.hideLoading();
        })
    },

    // 书籍
    stuBook(user_id) {
        var that = this;
        var objdata = {
            user_id: user_id, //是	int	班级学员ID
        }
        share.wxRequest('crmapi/user/stuBook', objdata, function(res) {
            if (Number(res.data.errcode) == 0) {
                var gets = res.data.gets;
                var noGets = res.data.noGets;
                var list = [];
                // gets 已领取  noGets 未领取
                if (Number(gets.count) > 0) {
                    list = list.concat(gets.goods);
                }
                if (Number(noGets.count) > 0) {
                    list = list.concat(noGets.goods);
                }
                that.setData({
                    booklist: list
                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})